package com.xydtech.workflow.controller;

import com.xydtech.common.annotation.Log;
import com.xydtech.common.core.controller.BaseController;
import com.xydtech.common.core.domain.AjaxResult;
import com.xydtech.common.core.page.TableDataInfo;
import com.xydtech.common.enums.BusinessType;
import com.xydtech.workflow.domain.WflFlowstep;
import com.xydtech.workflow.domain.WflSteppage;
import com.xydtech.workflow.service.IWflFlowstepService;
import com.xydtech.workflow.service.IWflSteppageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 流程节点页面配置Controller
 *
 * @author JHON
 * @date 2021-06-02
 */
@Controller
@RequestMapping("/workflow/steppage")
public class WflSteppageController extends BaseController {
    private String prefix = "workflow/steppage";

    @Autowired
    private IWflSteppageService wflSteppageService;
    @Autowired
    private IWflFlowstepService wflFlowstepService;
    //流程节点页面顺序上移
    private static String up = "up";
    //流程节点页面顺序下移
    private static String down = "down";

    /**
     * 查询流程节点页面配置列表
     */
    @RequiresPermissions("workflow:flowdefine:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WflSteppage wflSteppage) {
        startPage();
        List<WflSteppage> list = wflSteppageService.selectWflSteppageList(wflSteppage);
        return getDataTable(list);
    }

    /**
     * 新增保存流程节点页面配置
     */
    @RequiresPermissions("workflow:flowdefine:add")
    @Log(title = "新增流程节点页面配置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Long versionId, Long orderNum, String pageIds) {
        //根据版本号和序号查询流程节点信息
        WflFlowstep wflFlowstep = wflFlowstepService.selectWflFlowstepByVersionIdAndOrderNum(versionId, orderNum);
        //查询已配置页面信息
        WflSteppage oldWflSteppage = new WflSteppage();
        oldWflSteppage.setVersionId(versionId);
        oldWflSteppage.setStepId(wflFlowstep.getStepId());
        List<WflSteppage> oldPageList = wflSteppageService.selectWflSteppageList(oldWflSteppage);
        //加工新增页面信息
        List<WflSteppage> addPageList = new ArrayList<WflSteppage>();
        String[] pages = pageIds.split(",");
        for (int i = 0; i < pages.length; i++) {
            WflSteppage newWflSteppage = new WflSteppage();
            newWflSteppage.setVersionId(versionId);
            newWflSteppage.setStepId(wflFlowstep.getStepId());
            newWflSteppage.setPageId(Long.parseLong(pages[i]));
            //默认启用页面检查
            newWflSteppage.setIsCheck("1");
            //默认停用页面只读
            newWflSteppage.setIsOnlyRead("0");
            //页面序号在原有基础上递增
            newWflSteppage.setOrderNum((long) (oldPageList.size() + i + 1));
            addPageList.add(newWflSteppage);
        }
        return toAjax(wflSteppageService.insertWflSteppage(addPageList));
    }


    /**
     * 删除流程节点页面配置
     */
    @RequiresPermissions("workflow:flowdefine:remove")
    @Log(title = "删除流程节点页面", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long versionId, Long stepId, Long pageId) {
        try {
            //根据版本号节点号及页面编号删除数据
            WflSteppage wflSteppage = new WflSteppage();
            wflSteppage.setVersionId(versionId);
            wflSteppage.setStepId(stepId);
            wflSteppage.setPageId(pageId);
            wflSteppageService.deleteWflSteppageById(wflSteppage);
            //清空删除的页面编号
            wflSteppage.setPageId(null);
            //查询剩余页面配置信息
            List<WflSteppage> newPageList = wflSteppageService.selectWflSteppageList(wflSteppage);
            if (newPageList.size() > 0) {
                List<WflSteppage> lastPageList = new ArrayList<>();
                //遍历更新顺序号
                for (int i = 0; i < newPageList.size(); i++) {
                    WflSteppage lastWflSteppage = new WflSteppage();
                    lastWflSteppage = newPageList.get(i);
                    lastWflSteppage.setOrderNum((long) (i + 1));
                    lastPageList.add(lastWflSteppage);
                }
                wflSteppageService.batchUpdateWflSteppageOrderNum(lastPageList);
            }
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 移动流程节点页面顺序
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "移动流程节点页面顺序", businessType = BusinessType.UPDATE)
    @PostMapping("/move")
    @ResponseBody
    public AjaxResult move(Long versionId, Long stepId, Long pageId, Long orderNum, String moveType) {
        try {
            WflSteppage wflSteppage = new WflSteppage();
            wflSteppage.setVersionId(versionId);
            wflSteppage.setStepId(stepId);
            //上移
            if (up.equals(moveType)) {
                //当前节点上移替换的节点序号为当前节点序号orderNum-1
                wflSteppage.setOrderNum(orderNum - 1);
            } else if (down.equals(moveType)) {
                //查询页面配置信息
                List<WflSteppage> currentPageList = wflSteppageService.selectWflSteppageList(wflSteppage);
                if (orderNum == currentPageList.size()) {
                    return error("最后一个页面无法下移！");
                }
                //当前节点下移替换的节点序号为当前节点序号orderNum+1
                wflSteppage.setOrderNum(orderNum + 1);
            }
            //查询被替换位置页面信息
            List<WflSteppage> replacePage = wflSteppageService.selectWflSteppageList(wflSteppage);
            //被替换位置页面的编号
            Long replacePageId = replacePage.get(0).getPageId();
            //修改上被替换位置页面顺序号为当前页面顺序号
            wflSteppage.setPageId(replacePageId);
            wflSteppage.setOrderNum(orderNum);
            wflSteppageService.updateWflSteppage(wflSteppage);
            //更新当前页面顺序号为被替换位置的页面序号
            wflSteppage.setPageId(pageId);
            wflSteppage.setOrderNum(replacePage.get(0).getOrderNum());
            wflSteppageService.updateWflSteppage(wflSteppage);
            return success();
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改流程节点页面校验状态
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "修改流程节点页面校验状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeIsCheck")
    @ResponseBody
    public AjaxResult changeIsCheck(WflSteppage wflSteppage) {
        return toAjax(wflSteppageService.updateWflSteppage(wflSteppage));

    }

    /**
     * 修改流程节点页面只读状态
     */
    @RequiresPermissions("workflow:flowdefine:edit")
    @Log(title = "修改流程节点页面只读状态", businessType = BusinessType.UPDATE)
    @PostMapping("/changeIsOnlyRead")
    @ResponseBody
    public AjaxResult changeIsOnlyRead(WflSteppage wflSteppage) {
        return toAjax(wflSteppageService.updateWflSteppage(wflSteppage));
    }
}
